package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.*;

import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLikeApiImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Long queryLikeCount(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));

        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public Long queryFansCount(Long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));

        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public Long queryFriendsCount(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));

        return mongoTemplate.count(query, Friend.class);
    }

    public Double queryScore(long toUserId, long targetUserId){
        Query query = new Query(Criteria.where("toUserId").is(toUserId).and("userId").is(targetUserId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser != null) {
            return recommendUser.getScore();
        }
        return 95D;
    }
    public PageResult<CommonLikeUser> queryFriendsList(Long userId, int page, int pagesize) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(query, Friend.class);

        List<CommonLikeUser> userList = new ArrayList<>();
        for (Friend friend : friendList) {
            CommonLikeUser user = new CommonLikeUser();
            user.setTargetUserId(friend.getFriendId());
            user.setScore(queryScore(userId, user.getTargetUserId()));
            userList.add(user);
        }

        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int)count, pagesize, pages, page, userList);
    }
    @Override
    public PageResult<CommonLikeUser> queryLikeList(Long userId, int page, int pagesize) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);

        List<CommonLikeUser> commonUserList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            CommonLikeUser user = new CommonLikeUser();
            user.setTargetUserId(userLike.getLikeUserId());
            user.setScore(queryScore(userId, user.getTargetUserId()));
            commonUserList.add(user);
        }

        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int)count, pagesize, pages, page, commonUserList);
    }

    @Override
    public PageResult<CommonLikeUser> queryFansList(Long userId, int page, int pagesize) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);

        List<CommonLikeUser> commonUserList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            CommonLikeUser user = new CommonLikeUser();
            user.setTargetUserId(userLike.getUserId());
            user.setScore(queryScore(userId, user.getTargetUserId()));
            commonUserList.add(user);
        }

        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int)count, pagesize, pages, page, commonUserList);
    }

    @Override
    public PageResult<CommonLikeUser> findVisitorList(Long userId, int page, int pagesize) {
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("date")))
                .with(PageRequest.of(page-1, pagesize));

        List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);
        long count = mongoTemplate.count(query, Visitor.class);

        List<CommonLikeUser> commonUserList = new ArrayList<>();
        for (Visitor visitor : visitorList) {
            CommonLikeUser user = new CommonLikeUser();
            user.setTargetUserId(visitor.getVisitorUserId());
            user.setScore(queryScore(userId, user.getTargetUserId()));
            commonUserList.add(user);
        }

        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int)count, pagesize, pages, page, commonUserList);
    }

    @Override
    public void delete(Long userId, Long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        mongoTemplate.remove(query, UserLike.class);
    }

    /**
     * 探花-右划喜欢
     * @param loveId
     * @param userId
     */
    @Override
    public void tanhuaLove(Long loveId, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(loveId).and("likeUserId").is(userId));
        boolean exists = mongoTemplate.exists(query, UserLike.class);
        if (!exists) {
            query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(loveId));
            exists = mongoTemplate.exists(query, UserLike.class);
            if (!exists) {
                UserLike userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(loveId);
                userLike.setCreated(System.currentTimeMillis());
                mongoTemplate.insert(userLike);
            }
        }else {
            addFriend(loveId, userId);
        }
    }

    /**
     * 左滑不喜欢
     * @param unLoveId
     * @param userId
     */
    @Override
    public void tanhuaUnLove(Long unLoveId, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(unLoveId));
        mongoTemplate.remove(query, UserLike.class);
    }

    /**
     * 探花功能-如果对方也喜欢了用户,删除单向表再添加好友
     * @param loveId
     * @param userId
     */
    private void addFriend(Long loveId, Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(loveId).and("likeUserId").is(userId));
        mongoTemplate.remove(query, UserLike.class);
        query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("friendId").is(loveId));
        boolean exists = mongoTemplate.exists(query, Friend.class);
        if (!exists) {
            Friend friend = new Friend();
            friend.setUserId(userId);
            friend.setFriendId(loveId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.insert(friend);
        }
        query = new Query();
        query.addCriteria(Criteria.where("userId").is(loveId).and("friendId").is(userId));
        exists = mongoTemplate.exists(query, Friend.class);
        if (!exists) {
            Friend friend = new Friend();
            friend.setUserId(loveId);
            friend.setFriendId(userId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.insert(friend);
        }
    }



}